// 引入wx
import wx from "weixin-js-sdk";
import config from '@/config'
import request from '@/http'
/**
 * 微信内h5 跳转到app和微信右上角分享
 */
function wxChatJumpAppAndShare(a, shareData) {
  a = a || "99";
  const url =
    `${config.requestUrl}/cqliving-boot-sso/weChat/jsapiSignature?weChatId=${a}&url=` +
    encodeURIComponent(window.location.href.split("#")[0]);
  
    request
    .get(url, {
      baseURL: "https://gatewayapi.cqliving.com/cqliving-boot-sso"
    })
    .then(res => {
      const { code, data } = res;
      if (code >= 0) {
        const result = data;
        // 若要打印提示，则设置为true
        result.debug = false;
        wx.config({
          debug: false,
          appId: result.wxAppId,
          timestamp: result.timestamp,
          nonceStr: result.nonceStr,
          signature: result.signature,
          jsApiList: [
            "getLocation",
            "onMenuShareTimeline",
            "onMenuShareAppMessage",
            "onMenuShareQQ",
            "onMenuShareQZone",
          ],
        });
        
        wx.ready(() => {
          console.log("注入成功app");
          // 分享到朋友圈
          wx.onMenuShareTimeline({
            title: shareData.shareTitle,
            link: shareData.shareLink,
            imgUrl: shareData.shareImgUrl,
            desc: shareData.shareDesc,
            success: function() {},
            cancel: function() {}
          });
          // 分享给朋友
          wx.onMenuShareAppMessage({
            title: shareData.shareTitle,
            desc: shareData.shareDesc,
            link: shareData.shareLink,
            imgUrl: shareData.shareImgUrl,
            success: function() {},
            cancel: function() {}
          });
          // 分享到QQ
          wx.onMenuShareQQ({
            title: shareData.shareTitle,
            desc: shareData.shareDesc,
            link: shareData.shareLink,
            imgUrl: shareData.shareImgUrl,
            success: function() {},
            cancel: function() {}
          });
          // 分享到QQ空间
          wx.onMenuShareQZone({
            title: shareData.shareTitle,
            desc: shareData.shareDesc,
            link: shareData.shareLink,
            imgUrl: shareData.shareImgUrl,
            success: function() {},
            cancel: function() {}
          });
        });
        
        wx.error(function(e) {
          console.log("报错", e.detail);
        });
      }
    });
}


export default wxChatJumpAppAndShare;
// 注意：在实际使用中，确保已正确导入axios库，并且在环境中能够访问到wx对象。